<?php

namespace App\Http\Controllers\Api\Web\Flight;

use App\Http\Controllers\Common\ApiController;
use App\Models\Flight\FlightInfo;
use App\Models\Flight\Order;
use Illuminate\Http\Request;

class OrderInfoController extends ApiController{

    public function detail(Request $request){
        $code = $request->input('code',0);
        $this->validate($request, ['code'=>'required'],[],['code'=>'记录编码']);

        $records = Order::query()->where(['code'=>$code])
            ->whereHas('getCoupleFlightInfo', function ($q){
                $q->where('date','>=',date('Y-m-d'));
            })
            ->orderBy('id')
            ->take(2)
            ->with(['getFlightInfo','getCoupleFlightInfo'])->get(['id','code','flight_id','couple_flight_id','created_at']);

        foreach ($records as $record){
            $record->getFlightInfo->created_at = $record->created_at;
        }

        //删除已经过期的航班
        /*$filtered = $records->reject(function ($item) {
            return $item->date < date('Y-m-d');
        });*/

        if($records->isEmpty()){
            return responseFail('未查找到有效数据！');
        }

        $passengers = Order::query()->where(['code'=>$code])->get([
            'id','name','passenger_type','passenger_sex','passenger_birthday'
        ])->unique('name');

        return responseSuccess($records->toArray(),1,100000,compact('passengers'));
    }

}
